package authorizationRMI;

import java.io.PrintStream;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Map;
import java.util.TreeMap;


public class AuthorizationManager extends UnicastRemoteObject implements IAuthorizationRMI
{
    private static final long serialVersionUID = 1L;
    private Map<String, User> users;

    public AuthorizationManager(String as[]) throws RemoteException {
        users = new TreeMap<String, User>();
        System.out.println("Adding users ...");
        users.put("user1", new User("user1", "1111", "Federico Castanedo", 25, "male"));
        System.out.println("user1 - 1111 - Federico Castanedo - 25 - male");
        users.put("user2", new User("user2", "2222", "Diego L\363pez de Ipi\361a", 25, "male"));
        System.out.println("user2 - 2222 - Diego L\363pez de Ipi\361a - 25 - male");
   
        managerRegistry(as);
    }

    public void managerRegistry(String as[])
    {
        if(System.getSecurityManager() == null)
            System.setSecurityManager(new RMISecurityManager());
        String s = (new StringBuilder()).append("//").append(as[0]).append(":").append(as[1]).append("/").append(as[2]).toString();
        try
        {
            Naming.rebind(s, this);
            System.out.println("Authorization RMI Server active and waiting...");
        }
        catch(Exception exception)
        {
            System.err.println((new StringBuilder()).append("AuthorizationManager rebinding to the Registry: ").append(exception.getMessage()).toString());
        }
    }

    public User login(String s, String s1) throws RemoteException, InvalidUserException, InvalidPasswordException
    {
        User user = (User)users.get(s);
        System.out.println((new StringBuilder()).append("Request for User authorization: ").append(s).toString());
        if(user == null)
        {
            System.out.println((new StringBuilder()).append(" Invalid Username Exception ...").append(s).toString());
            throw new InvalidUserException("[InvalidUserException] Non existent user ...");
        }
        if(!user.getPassword().equals(s1))
        {
            System.out.println((new StringBuilder()).append(" Invalid Password Exception ...").append(s1).toString());
            throw new InvalidPasswordException("[InvalidPasswordException] Invalid password ...");
        } else
        {
            System.out.println((new StringBuilder()).append("Authorization OK ...").append(user.getName()).toString());
            return user;
        }
    }

    public static void main(String args[])
    {
        if(args.length != 3)
        {
            System.out.println("Execute: java [policy] [codebase] -jar authorizationRMI.jar [host] [port] [server]");
            System.exit(0);
        }
        try
        {
            new AuthorizationManager(args);
        }
        catch(Exception exception)
        {
            System.err.println((new StringBuilder()).append("AuthorizationManager.main(): ").append(exception.getMessage()).toString());
        }
    }
}
